/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.managedbeans;

import com.blog.city.checkthedep.entities.Article;
import com.blog.city.checkthedep.entities.Comment;
import com.blog.city.checkthedep.entities.Tag;
import com.blog.city.checkthedep.sessionbeans.SearchLocal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Breath
 * Search for some strange reason work on the second time you want to perform a search
 */
@ManagedBean
@RequestScoped
public class SearchBean {

    private String keyword = "";
    private List<Object> results = null;
    @EJB
    private SearchLocal searchFac;

    /**
     * 
     * @return
     */
    public String getKeyword() {
        return keyword;
    }

    /**
     * 
     * @param keyword
     */
    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    /**
     * 
     * @param query
     * @return
     */
    public List<String> complete(String query) {
        List<String> toString = new ArrayList<String>();
        List<Article> articles = new LinkedList<Article>();
        List<Article> searchedArticles = new LinkedList<Article>();
        List<Tag> tags = new LinkedList<Tag>();
        List<Comment> comments = new LinkedList<Comment>();

        for (int i = 0; i < searchFac.searchByKeyword(query).size(); i++) {
            if (searchFac.searchByKeyword(query).get(i) instanceof Article) {
                articles.add((Article) searchFac.searchByKeyword(query).get(i));
            }
            if (searchFac.searchByKeyword(query).get(i) instanceof Tag) {
                tags.add((Tag) searchFac.searchByKeyword(query).get(i));
            }
            if (searchFac.searchByKeyword(query).get(i) instanceof Comment) {
                comments.add((Comment) searchFac.searchByKeyword(query).get(i));
            }
        }

        for (int i = 0; i < articles.size(); i++) {
            if (articles.get(i).getStatus().equals("Published")) {
                searchedArticles.add(articles.get(i));
            }
        }

        for (int i = 0; i < searchedArticles.size(); i++) {
            toString.add("Article with title " + searchedArticles.get(i).getTitle());
        }

        for (int i = 0; i < tags.size(); i++) {
            toString.add("Tag with name " + tags.get(i).getName());
        }

        for (int i = 0; i < comments.size(); i++) {
            toString.add("Comment " + comments.get(i).getCommentBody());
        }
        return toString;
    }

    /**
     * 
     * @return
     */
    public List<Object> getResults() {
        return results;
    }

    /**
     * 
     * @param results
     */
    public void setResults(List<Object> results) {
        this.results = results;
    }

    /**
     * 
     */
    public void handleSelect() {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected:", null);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}
